package redis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.map.HashedMap;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import spring.po.User;

public class RedisTest {

	private static String lockName = "testYxp";
	
	private static Lock lock = new ReentrantLock();

	private static int sum = 0;
	public static void main(String[] args) {
		RedisUtils.init();
//		redis-cli -p 9488 -a DN1XOzA+AAwmO+3TSsB7M/Xr9ItE=
		
		
//		ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);
//		ScheduledExecutorService scheduledExecutorService1 = Executors.newScheduledThreadPool(5);
//		scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
//
//			@Override
//			public void run() {
//				addPersonNum(1, 20181217);
//				System.out.println(getPersonNum(20181217) + " --- " + Thread.currentThread().getName());
//			}
//		}, 1, 1, TimeUnit.SECONDS);
//		try {
//			Thread.sleep(600);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		scheduledExecutorService1.scheduleAtFixedRate(new Runnable() {
//
//			@Override
//			public void run() {
//				try {
//					Thread.sleep(600);
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//				addPersonNum(1, 20181217);
//				System.out.println(getPersonNum(20181217) + " --- " + Thread.currentThread().getName());
//			}
//		}, 1, 1, TimeUnit.SECONDS);
		
		
//		ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
//		for (int i = 0; i < 100; i++) {
//			fixedThreadPool.execute(new Runnable() {
//				public void run() {
////					lock.lock();
//					addPersonNum(1, 20181217);
//					System.out.println(getPersonNum(20181217));
////					lock.unlock();
//				}
//			});
//		}
//		fixedThreadPool.shutdown();
//		ExecutorService fixedThreadPool2 = Executors.newFixedThreadPool(3);
//		for (int i = 0; i < 100; i++) {
//			fixedThreadPool2.execute(new Runnable() {
//				public void run() {
//					addPersonNum2(1, 20181217);
//					System.out.println(getPersonNum(20181217));
//				}
//			});
//		}
//		fixedThreadPool2.shutdown();
		StringBuilder redisPersonKey = new StringBuilder();
        redisPersonKey.append(RedisKeys.K_ALL_SERVER_GIFT).append("_").append(202099);
		for (int i = 0; i < 10; i++) {
			RedisUtils.hset(redisPersonKey.toString(), "No" + i, String.valueOf(10 + i));
		}
		for (int i = 0; i < 10; i++) {
			RedisUtils.hincrBy(redisPersonKey.toString(), "No" + i, i);
		}
		RedisUtils.hincrBy(redisPersonKey.toString(), "No" + 1000, 100);
		
		Map<String, String> allNum = RedisUtils.hgetAll(redisPersonKey.toString());
		allNum.values().forEach(e->{
			sum += Integer.parseInt(e);
		});
		System.out.println(sum);
	}

	public static void addPersonNum(int value, int activityId) {
		// k_all_server_gift_20181217
		String redisPersonKey = RedisKeys.K_ALL_SERVER_GIFT + "_" + activityId;
		try {
//			if (RedisUtils.acquireLock(lockName, redis)) {
//				RedisUtils.hincrBy(redisPersonKey, "all", value);
			lock.lock();
//			try {
//				Thread.sleep(1000);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
				RedisUtils.getJedis().hincrBy(redisPersonKey, "all", value);
				lock.unlock();
//			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}

	}
	public static void addPersonNum2(int value, int activityId) {
		// k_all_server_gift_20181217
		String redisPersonKey = RedisKeys.K_ALL_SERVER_GIFT + "_" + activityId;
		try {
//			if (RedisUtils.acquireLock(lockName, redis)) {
//				RedisUtils.hincrBy(redisPersonKey, "all", value);
			lock.lock();
			RedisUtils.getJedis().hincrBy(redisPersonKey, "all", value);
			lock.unlock();
//			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		
	}
	
	public static int getPersonNum(int activityId) {
		lock.lock();
		String redisPersonKey = RedisKeys.K_ALL_SERVER_GIFT + "_" + activityId;
//		int a = Integer.parseInt(RedisUtils.hget(redisPersonKey, "all"));
		int a = Integer.parseInt(RedisUtils.getJedis().hget(redisPersonKey, "all"));
		lock.unlock();
		return a;
	}

	@Test
	public void insertMapList(){
		RedisUtils.init("127.0.0.1", 6379, "");
		Map<String, List<String>> stringListMap = new HashedMap();
		List<String> strings = new ArrayList<>();
		strings.add("daf");
		strings.add("daffff");
		stringListMap.put("fas1", strings);
		stringListMap.put("fas2", strings);

		Map<String, String> tempMap = new HashedMap();
		tempMap.put("table1", JSONObject.toJSONString(stringListMap));
		RedisUtils.getJedis().hmset("df", tempMap);

		String hget = RedisUtils.getJedis().hget("df", "table1");
		HashMap hashMap = JSON.parseObject(hget, HashMap.class);
		System.out.println(hashMap);

	}

	@Test
	public void insertListString(){
		RedisUtils.init("127.0.0.1", 6379, "");
		List<User> strings = new ArrayList<>();
		User user = User.buildUser();
		strings.add(user);
		RedisUtils.getJedis().set("df", JSONObject.toJSONString(strings));

		String hget = RedisUtils.getJedis().get("df");
		List<String> hashMap = JSON.parseObject(hget, List.class);
		System.out.println(hashMap);

	}

	@Test
	public void rPushList(){
		RedisUtils.init("127.0.0.1", 6379, "");
		User user = User.buildUser();
		user.setName("xxp1");
		RedisUtils.getJedis().rpush("df1", JSON.toJSONString(user));
		user.setName("xxp2");
		RedisUtils.getJedis().rpush("df1", JSON.toJSONString(user));

		String hget1 = RedisUtils.getJedis().rpop("df1");
		User hashMap1 = JSON.parseObject(hget1, User.class);
		System.out.println(hashMap1);

		String hget2 = RedisUtils.getJedis().rpop("df1");
		User hashMap2 = JSON.parseObject(hget2, User.class);
		System.out.println(hashMap2);

	}

	@Test
	public void swapTest() {
		int a = 100;
		int b = 200;
		User user = User.buildUser();
		System.out.println(a + " " + b + " " + user);
		swap(a, b, user);
		System.out.println(a + " " + b + " " + user);

		float c = 0.99f;

		System.out.println((int)(2 * c));
	}

	void swap(int a, int b, User user) {
		int temp = a;
		a = b;
		b = temp;
		user.setName("fafsdf");
	}

	@Test
	public void listTest(){
		List<User> userList = new ArrayList<>();
		userList.add(null);
		for (User user: userList) {
			System.out.println(user.getAge());
		}
	}
	
	
}
